<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Preparation</title>
        <style type="text/css">
            body {
                font-family: Arial;
            }
            .table1 {
                width: 100%;
            }
            .table1 th {
                text-align: left;
                font-size: 12px;
            }
            .table1 td {
                vertical-align: top;
                font-size: 12px;
            }
            .table1 td textarea{
                width: 550px;
                height: 60px;
                font-family: Arial;
                font-size: 12px;
            }
            .table1 td input{
                width: 550px;
            }
        </style>
    </head>
    <body style="width: 1260px;">
        <?php
        require_once 'includes/php/connect_db.php';
        $sql = "SELECT * FROM tbl_0950_page_list ORDER BY page_name";
        if ( !$res = mysql_query($sql) ) {
            die("Error in line " . __LINE__ . ":<br>" . mysql_error());
        }
        while ( $row = mysql_fetch_assoc($res) ) {
            $page_list[] = array('id' => $row['lng_k_page_list'], 'name' => $row['page_name'], 'prefix' => $row['str_var_prefix']);
            $page_prefixes[$row['lng_k_page_list']] = $row['str_var_prefix'];
        }
        $sql = "SELECT * FROM tbl_0952_text_group ORDER BY str_group_name";
        if ( !$res = mysql_query($sql) ) {
            die("Error in line " . __LINE__ . ":<br>" . mysql_error());
        }
        while ( $row = mysql_fetch_assoc($res) ) {
            $text_group_list[] = array('id' => $row['lng_k_text_group'], 'name' => $row['str_group_name'], 'description' => $row['str_description']);
        }
        $sql = "SELECT * FROM tbl_0951_text_type ORDER BY st_text_type";
        if ( !$res = mysql_query($sql) ) {
            die("Error in line " . __LINE__ . ":<br>" . mysql_error());
        }
        while ( $row = mysql_fetch_assoc($res) ) {
            $text_type_list[] = array('id' => $row['lng_k_text_type'], 'type' => $row['st_text_type'], 'description' => $row['str_description']);
        }
        $last = 0;
        $first = 0;
        $save_disable = 0;
        $new_disable = 0;
        $clear_disable = 0;
        if ( $_REQUEST['action'] != "" && $_REQUEST['page_name'] != '-1' ) {
            if ( $_REQUEST['action'] == 'load_first' ) {
                $sql = "SELECT * FROM tbl_0953_text_translations 
                WHERE lng_fk_page_list = '{$_REQUEST['page_name']}' 
                AND i_record_status = 0 
                ORDER BY str_k_var_number LIMIT 1";
                $first = 1;
                if ( !$res = mysql_query($sql) ) {
                    die("Error in line " . __LINE__ . ":<br>" . mysql_error());
                }
                if ( !$data = mysql_fetch_assoc($res) ) {
                    $selected_items['page_list'] = $_REQUEST['page_name'];
                    $selected_items['text_group_list'] = '-1';
                    $selected_items['text_type_list'] = '-1';
                    $id = get_new_id($_REQUEST['page_name']);
                    $record_name = $page_prefixes[$_REQUEST['page_name']] . "_" . ($id < 10 ? "0" . $id : $id);
                    $record_id = "-1";
                    $last = 1;
                } else {
                    $selected_items['page_list'] = $data['lng_fk_page_list'];
                    $selected_items['text_group_list'] = $data['lng_fk_text_group'];
                    $selected_items['text_type_list'] = $data['lng_fk_text_type'];
                    $record_name = $page_prefixes[$data['lng_fk_page_list']] . "_" . ($data['str_k_var_number'] < 10 ? "0" . $data['str_k_var_number'] : $data['str_k_var_number']);
                    $record_id = $data['str_k_var_number'];
                }
            } else if ( $_REQUEST['action'] == 'new' ) {
                $sql = "SELECT * FROM tbl_0953_text_translations 
                WHERE lng_fk_page_list = '{$_REQUEST['page_name']}' 
                AND i_record_status = 0 
                ORDER BY str_k_var_number LIMIT 1";
                if ( !$res = mysql_query($sql) ) {
                    die("Error in line " . __LINE__ . ":<br>" . mysql_error());
                }
                if ( mysql_num_rows($res) == 0 ) {
                    $first = 1;
                }
                $selected_items['page_list'] = $_REQUEST['page_name'];
                $selected_items['text_group_list'] = '-1';
                $selected_items['text_type_list'] = '-1';
                $id = get_new_id($_REQUEST['page_name']);
                $record_name = $page_prefixes[$_REQUEST['page_name']] . "_" . ($id < 10 ? "0" . $id : $id);
                $record_id = "-1";
                $new_disable = 1;
                $last = 1;
            } else if ( $_REQUEST['action'] == 'next' ) {
                $sql = "SELECT * FROM tbl_0953_text_translations 
                WHERE lng_fk_page_list = '{$_REQUEST['page_name']}' 
                AND str_k_var_number > '{$_REQUEST['record_id']}'
                AND i_record_status = 0
                ORDER BY str_k_var_number ASC LIMIT 2";
                if ( !$res = mysql_query($sql) ) {
                    die("Error in line " . __LINE__ . ":<br>" . mysql_error());
                }
                if ( !$data = mysql_fetch_assoc($res) ) {
                    $selected_items['page_list'] = $_REQUEST['page_name'];
                    $selected_items['text_group_list'] = '-1';
                    $selected_items['text_type_list'] = '-1';
                    $id = get_new_id($_REQUEST['page_name']);
                    $record_name = $page_prefixes[$_REQUEST['page_name']] . "_" . ($id < 10 ? "0" . $id : $id);
                    $record_id = "-1";
                    $last = 1;
                } else {

                    $selected_items['page_list'] = $data['lng_fk_page_list'];
                    $selected_items['text_group_list'] = $data['lng_fk_text_group'];
                    $selected_items['text_type_list'] = $data['lng_fk_text_type'];
                    $record_name = $page_prefixes[$data['lng_fk_page_list']] . "_" . ($data['str_k_var_number'] < 10 ? "0" . $data['str_k_var_number'] : $data['str_k_var_number']);
                    $record_id = $data['str_k_var_number'];
                }
            } else if ( $_REQUEST['action'] == 'prev' ) {
                if ( $_REQUEST['record_id'] == '-1' ) {
                    $sql = "SELECT * FROM tbl_0953_text_translations 
                    WHERE lng_fk_page_list = '{$_REQUEST['page_name']}'
                    AND i_record_status = 0
                    ORDER BY str_k_var_number DESC LIMIT 2";
                } else {
                    $sql = "SELECT * FROM tbl_0953_text_translations 
                    WHERE lng_fk_page_list = '{$_REQUEST['page_name']}' 
                    AND str_k_var_number < '{$_REQUEST['record_id']}'
                    AND i_record_status = 0
                    ORDER BY str_k_var_number DESC LIMIT 2";
                }
                if ( !$res = mysql_query($sql) ) {
                    die("Error in line " . __LINE__ . ":<br>" . mysql_error());
                }
                if ( !$data = mysql_fetch_assoc($res) ) {
                    die($sql);
                } else {
                    $selected_items['page_list'] = $data['lng_fk_page_list'];
                    $selected_items['text_group_list'] = $data['lng_fk_text_group'];
                    $selected_items['text_type_list'] = $data['lng_fk_text_type'];
                    if ( $_REQUEST['action'] == 'prev' && mysql_num_rows($res) != 2 ) {
                        $first = 1;
                    }
                    $record_name = $page_prefixes[$data['lng_fk_page_list']] . "_" . ($data['str_k_var_number'] < 10 ? "0" . $data['str_k_var_number'] : $data['str_k_var_number']);
                    $record_id = $data['str_k_var_number'];
                }
            } else if ( $_REQUEST['action'] == 'save' || $_REQUEST['action'] == 'save_new' ) {
                $_REQUEST['english_old'] = utf8_decode($_REQUEST['english_old']);
                $_REQUEST['eng_link'] = utf8_decode($_REQUEST['eng_link']);
                $_REQUEST['eng_comments'] = utf8_decode($_REQUEST['eng_comments']);
                $_REQUEST['german_old'] = utf8_decode($_REQUEST['german_old']);
                $_REQUEST['ger_link'] = utf8_decode($_REQUEST['ger_link']);
                $_REQUEST['ger_comments'] = utf8_decode($_REQUEST['ger_comments']);
                if ( $_REQUEST['record_id'] == '-1' ) {
                    $id = get_new_id($_REQUEST['page_name']);
                    $sql = "
                    INSERT INTO tbl_0953_text_translations SET
                        str_k_var_number = '{$id}',
                        str_k_var_name = '{$_REQUEST['record_name']}',
                        lng_fk_text_group = '{$_REQUEST['text_group']}',
                        lng_fk_text_type = '{$_REQUEST['text_type']}',
                        str_old_english = '".mysql_real_escape_string($_REQUEST['english_old'])."',
                        lng_fk_text_status_en = '".($_REQUEST['english_old'] != ""?"1":"0")."',
                        str_new_english_link = '{$_REQUEST['eng_link']}',
                        str_english_comments = '{$_REQUEST['eng_comments']}',
                        str_old_german = '".mysql_real_escape_string($_REQUEST['german_old'])."',
                        lng_fk_text_status_ge = '".($_REQUEST['german_old'] != ""?"1":"0")."',
                        str_new_german_link = '{$_REQUEST['ger_link']}',
                        str_german_comments = '{$_REQUEST['ger_comments']}',
                        lng_fk_page_list = '{$_REQUEST['page_name']}',
						str_new_german_draft = '".mysql_real_escape_string($_REQUEST['german_old'])."',
						str_new_english_draft = '".mysql_real_escape_string($_REQUEST['english_old'])."'
                    ";
                    if ( !$res = mysql_query($sql) ) {
                        die("Error in line " . __LINE__ . ":<br>" . mysql_error() . "<br>" . $sql);
                    }
                } else {
                    $sql = "
                    UPDATE tbl_0953_text_translations SET
                        lng_fk_text_group = '{$_REQUEST['text_group']}',
                        lng_fk_text_type = '{$_REQUEST['text_type']}',
                        str_old_english = '".mysql_real_escape_string($_REQUEST['english_old'])."',
                        lng_fk_text_status_en = '".($_REQUEST['english_old'] != ""?"1":"0")."',
                        str_new_english_link = '{$_REQUEST['eng_link']}',
                        str_english_comments = '{$_REQUEST['eng_comments']}',
                        str_old_german = '".mysql_real_escape_string($_REQUEST['german_old'])."',
                        lng_fk_text_status_ge = '".($_REQUEST['german_old'] != ""?"1":"0")."',
                        str_new_german_link = '{$_REQUEST['ger_link']}',
                        str_german_comments = '{$_REQUEST['ger_comments']}',
                        str_k_var_name = '{$_REQUEST['record_name']}'
                    WHERE str_k_var_number = '{$_REQUEST['record_id']}'
                    AND lng_fk_page_list = '{$_REQUEST['page_name']}'
                    ";
                    if ( !$res = mysql_query($sql) ) {
                        die("Error in line " . __LINE__ . ":<br>" . mysql_error());
                    }
                    $id = $_REQUEST['record_id'];
                }
                $sql = "SELECT * FROM tbl_0953_text_translations 
                WHERE lng_fk_page_list = '{$_REQUEST['page_name']}' 
                AND str_k_var_number < '$id' 
                AND i_record_status = 0
                ORDER BY str_k_var_number DESC LIMIT 2";
                if ( !$res = mysql_query($sql) ) {
                    die("Error in line " . __LINE__ . ":<br>" . mysql_error());
                }
                if ( mysql_num_rows($res) != 2 ) {
                    $first = 1;
                }
                if ( $_REQUEST['action'] == 'save_new' ) {
                    $selected_items['page_list'] = $_REQUEST['page_name'];
                    $selected_items['text_group_list'] = '-1';
                    $selected_items['text_type_list'] = '-1';
                    $id = get_new_id($_REQUEST['page_name']);
                    $record_name = $page_prefixes[$_REQUEST['page_name']] . "_" . ($id < 10 ? "0" . $id : $id);
                    $record_id = "-1";
                    $last = 1;
                    $first = 1;
                } else {
                    $sql = "
                    SELECT * 
                    FROM tbl_0953_text_translations 
                    WHERE str_k_var_number = '$id'
                    AND lng_fk_page_list = '{$_REQUEST['page_name']}'
                    AND i_record_status = 0 LIMIT 1";
                    if ( !$res = mysql_query($sql) ) {
                        die("Error in line " . __LINE__ . ":<br>" . mysql_error());
                    }
                    $data = mysql_fetch_assoc($res);
                    $selected_items['page_list'] = $_REQUEST['page_name'];
                    $selected_items['text_group_list'] = $_REQUEST['text_group'];
                    $selected_items['text_type_list'] = $_REQUEST['text_type'];
                    $record_name = $page_prefixes[$_REQUEST['page_name']] . "_" . ($id < 10 ? "0" . $id : $id);
                    $record_id = $id;
                }
            }
        } else {
            $selected_items['page_list'] = "-1";
            $selected_items['text_group_list'] = '-1';
            $selected_items['text_type_list'] = '-1';
            $record_name = '';
            $record_id = "-1";
            $last = 1;
            $first = 1;
            $save_disable = 1;
            $new_disable = 1;
            $clear_disable = 1;
        }

        function get_new_id($page_id) {
            $sql = "SELECT MAX(str_k_var_number) ID FROM tbl_0953_text_translations WHERE i_record_status = 0 AND lng_fk_page_list = '$page_id'";
            if ( !$res = mysql_query($sql) ) {
                die("Error in line " . __LINE__ . ":<br>" . mysql_error());
            }
            $row = mysql_fetch_assoc($res);
            return $row['ID'] + 1;
        }
        ?>
        <script type="text/javascript">
            var page_list = <?= json_encode($page_list) ?>;
            var text_group_list = <?= json_encode($text_group_list) ?>;
            var text_type_list = <?= json_encode($text_type_list) ?>;
            var selected_items = <?= json_encode($selected_items) ?>;
            var last = '<?= $last ?>';
            var first = '<?= $first ?>';
            var save_disable = '<?= $save_disable ?>';
            var new_disable = '<?= $new_disable ?>';
            var clear_disable = '<?= $clear_disable ?>';
            var modified = false;
            
            function getObj(id){
                return document.getElementById(id);
            }
            function addElement(tgt_select, option_text, option_value)	{
                var oOption = document.createElement("option");
                oOption.value = option_value;
                oOption.appendChild(document.createTextNode(option_text));
                tgt_select.appendChild(oOption);
            }
            
            function fill_comboboxes(){
                var cbxPageList = getObj('page_name');
                var cbxTextGroup = getObj('text_group');
                var cbxTextType = getObj('text_type');
                for (var i = 0; i < page_list.length; i++) {
                    addElement(cbxPageList, page_list[i].name, page_list[i].id);
                }
                cbxPageList.value = selected_items.page_list;
                for (var i = 0; i < text_group_list.length; i++) {
                    addElement(cbxTextGroup, text_group_list[i].name, text_group_list[i].id);
                }
                //cbxTextGroup.value = selected_items.text_group_list;
                cbxTextGroup.value = 1;
                for (var i = 0; i < text_type_list.length; i++) {
                    addElement(cbxTextType, text_type_list[i].type, text_type_list[i].id);
                }
                //cbxTextType.value = selected_items.text_type_list;
                cbxTextType.value = 0;
            }
            
            function load_data(row){
                if (row == undefined){
//                    getObj('action').value = 'load_first';
                      getObj('action').value = 'new';
                } else {
                    getObj('action').value = row;
                }
                if (row == 'new' && modified){
                    if ( confirm('Your current text is modified.\nClick OK to save the modifications and open the new text page.\nClick Cancel to lose the modifications and open the new text page.') ){
                        if (getObj('text_group').value == "-1" || getObj('text_type').value == "-1"){
                            alert('Cannot save the current text. Not all the required fields are filled.');
                            return;
                        } else {
                            getObj('action').value = 'save_new'
                        }
                    }
                }
                getObj('frmMain').submit();
            }
                        
            function clear_form(){
                if (! confirm('All data will be cleared! Continue?')){return}
                var form = document.getElementById('frmMain');
                for (var i = 0; i < form.length; i++){
                    if (form.elements[i].type != "button"){
                        if (form.elements[i].tagName == "SELECT" || form.elements[i].name == 'record_id'){
                            form.elements[i].value = "-1";
                        } else {
                            form.elements[i].value = "";
                        }
                    } else {
                        form.elements[i].disabled = true;
                    }
                }
            }
            
            function validateForm(){
                if (getObj('english_old').value != "" || getObj('german_old').value != ""){
                    getObj('btnNew').disabled = false;
                    if (getObj('text_group').value != "-1" && getObj('text_type').value != "-1" && modified){
                        getObj('btnSave').disabled = false;
                    } else {
                        getObj('btnSave').disabled = true;
                    }
                } else {
                    getObj('btnNew').disabled = true;
                    getObj('btnSave').disabled = true;
                }
            }
            
            window.onload = function(){
                fill_comboboxes();
                if (last == '1'){
                    getObj('btnNext').disabled = true;
                } else {
                    getObj('btnNext').disabled = false;
                }
                if (first == '1'){
                    getObj('btnPrev').disabled = true;
                } else {
                    getObj('btnPrev').disabled = false;
                }
                if (save_disable == '1'){
                    getObj('btnSave').disabled = true;
                } else {
                    getObj('btnSave').disabled = false;
                }
                if (new_disable == '1'){
                    getObj('btnNew').disabled = true;
                } else {
                    getObj('btnNew').disabled = false;
                }
                if (clear_disable == '1'){
                    getObj('btnClear').disabled = true;
                } else {
                    getObj('btnClear').disabled = false;
                }
                validateForm();
            }
        </script>
        <h1 style="text-align: center;">Preparation</h1>
        <form name="frmMain" id="frmMain" method="post" action="" style="padding: 20px;">
            <input type="hidden" name="action" id="action" value="" />
            <table border="0" class="table1" >
                <tr>
                    <th style="width: 250px;">Select Page</th>
                    <th style="width: 360px;">Text record name in DB Table</th>
                    <th style="width: 180px;">Text Group</th>
                    <th style="width: 180px;">Text Type</th>
                    <th>&nbsp;</th>
                </tr>
                <tr>
                    <td>
                        <select name="page_name" id="page_name" style="" onChange="load_data();">
                            <option value="-1">Please select...</option>
                        </select>
                    </td>
                    <td>
                        <input type="hidden" name="record_id" value="<?= $record_id ?>" />
                        <input readonly="true" type="text" name="record_name" id="record_name" style="width: 300px;" value="<?= $record_name ?>" />
                    </td>
                    <td>
                        <select name="text_group" id="text_group" style="" onChange="modified = true; validateForm();">
                            <option value="-1">Please select...</option>
                        </select>
                    </td>
                    <td>
                        <select name="text_type" id="text_type" style="" onChange="modified = true; validateForm();">
                            <option value="-1">Please select...</option>
                        </select>
                    </td>
                    <td>&nbsp</td>
                </tr>
            </table>
            <table border="0" class="table1" style="margin-top: 20px;">
                <tr>
                    <th style="padding-right: 20px;">English OLD</th>
                    <th>Comments</th>
                </tr>
                <tr>
                    <td style="padding-right: 20px;">
                        <textarea style="height: 82px;" name="english_old" id="english_old" onKeyUp="modified = true; validateForm();" onChange=""><?= utf8_encode($data['str_old_english']) ?></textarea>
                    </td>
                    <td style="vertical-align: top;">
                        <textarea style="height: 30px;" name="eng_comments" id="eng_comments" onChange="modified = true;"><?= utf8_encode($data['str_english_comments']) ?></textarea>
                        <br />
                        <p style="margin-bottom: 2px;"><b>English-Link</b></p>
                        <input name="eng_link" type="text" style="" value="<?= utf8_encode($data['str_new_english_link']) ?>" onChange="modified = true;" />
                    </td>
                </tr>
            </table>
            <hr style="color: #333333;" />
            <table border="0" class="table1" style="margin-top: 20px;">
                <tr>
                    <th style="padding-right: 20px;">German OLD</th>
                    <th>Comments</th>
                </tr>
                <tr>
                    <td style="padding-right: 20px;">
                        <textarea style="height: 82px;" name="german_old" id="german_old" onKeyUp="modified = true; validateForm();" onChange=""><?= utf8_encode($data['str_old_german']) ?></textarea>
                    </td>
                    <td>
                        <textarea style="height: 30px;" name="ger_comments" id="ger_comments" onChange="modified = true;"><?= utf8_encode($data['str_german_comments']) ?></textarea>
                        <br />
                        <p style="margin-bottom: 2px;"><b>German-Link</b></p>
                        <input name="ger_link" type="text" style="" value="<?= utf8_encode($data['str_new_german_link']) ?>" onChange="modified = true;" />
                    </td>
                </tr>
            </table>
            <div style="text-align: right; padding-top: 20px;">
                <input style="width: 70px;" id="btnPrev" type="button" value="Previous" onClick="load_data('prev');" />
                <input style="width: 70px;" id="btnNext" type="button" value="Next" onClick="load_data('next');" />
                <input style="width: 70px;" id="btnNew" type="button" value="New" onClick="load_data('new');" />
                <input style="width: 70px;" id="btnClear" type="button" value="Clear" onClick="clear_form();" />
                <input style="width: 70px;" id="btnSave" type="button" value="Save" onClick="load_data('save');" />
            </div>
        </form>
    </body>
</html>
